package com.ysh.xiaomi.utils;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 *
 * 抽取工具类
 * @author 千锋教育
 * @Company http://www.mobiletrain.org/
 * @Version 1.0
 */
public class DbUtils {
    //在每一个线程 中都会创建一个副本
    private static final ThreadLocal<Connection> tl = new ThreadLocal<>();

    static {
        //1. 加载驱动类
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            System.out.println("加载驱动类失败");
            e.printStackTrace();
        }
    }

    /**
     * 获取 连接对象
     * @return
     */
    public static Connection getConn() {
        //从本地线程副本中获取连接对象
        Connection conn = tl.get();

        //如果没有连接对象，创建对象，并添加到线程副本中
        if(conn == null) {
            //加载Properties属性文件
            //获取 字节输入流对象
            InputStream is = DbUtils.class.getResourceAsStream("/db.properties");
            Properties prop = new Properties();
            try {
                prop.load(is);
            } catch (IOException e) {
                e.printStackTrace();
            }


            try {
//                conn = DriverManager.getConnection(prop.getProperty("url"), prop.getProperty("user"), prop.getProperty("password"));
//                System.out.println("创建Connection对象成功!!");
                conn = DruidDataSourceUtils.getConn();
                //设置 连接对象到 线程副本中
                tl.set(conn);
                return tl.get();
            } catch (SQLException e) {
                System.out.println("创建Connection对象失败");
                e.printStackTrace();
            }
        }


        return conn;
    }

    /**
     * 关闭所有资源
     * ResultSet , Statement , Connection
     */
    public static void closeAll(Connection conn , Statement sta, ResultSet rs) {

        if(rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(sta != null) {
            try {
                sta.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(conn != null) {
            try {
                //从线程副本中把连接移除
                tl.remove();
                conn.setAutoCommit(true);
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }


    public static void beginTransaction() {
        Connection conn = getConn();
        try {
            //设置不能自动提交
            conn.setAutoCommit(false);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void commitTransaction() {
        Connection conn = getConn();
        try {
            conn.commit();

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll(conn, null, null);
        }
    }

    public static void rollback() {
        Connection conn = getConn();
        try {
            conn.rollback();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll(conn, null, null);
        }
    }

}
